package com.clarte.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.apache.log4j.Logger;

public class CopyFile {
	public static void main(String[] args) throws Exception {
		Logger log = Logger.getLogger(CopyFile.class);
		log.info("aaaa");
		new Taa("ttttttt").start();
		log.info("bbbbb");
	}

	public void coypFileNio(String infile, String outfile) throws Exception {
		long st = System.currentTimeMillis();
		// 获取源文件和目标文件的输入输出流
		FileInputStream fin = new FileInputStream(infile);
		FileOutputStream fout = new FileOutputStream(outfile);
		// 获取输入输出通道
		FileChannel fcin = fin.getChannel();
		FileChannel fcout = fout.getChannel();
		// 创建缓冲区
		ByteBuffer buffer = ByteBuffer.allocate(2048);
		while (true) {
			// clear方法重设缓冲区，使它可以接受读入的数据
			buffer.clear();
			// 从输入通道中将数据读到缓冲区
			int r = fcin.read(buffer);
			// read方法返回读取的字节数，可能为零，如果该通道已到达流的末尾，则返回-1
			if (r == -1) {
				break;
			}
			// flip方法让缓冲区可以将新读入的数据写入另一个通道
			buffer.flip();
			// 从输出通道中将数据写入缓冲区
			fcout.write(buffer);
		}
		fin.close();
		fcin.close();
		fout.close();
		fcout.close();
		long et = System.currentTimeMillis();
		System.out.println(et - st);
	}
	
	public void coypDirNio(String infile, String outfile) throws Exception {
		long st = System.currentTimeMillis();
		// 获取源文件和目标文件的输入输出流
		FileOutputStream fout = new FileOutputStream(outfile);
		FileChannel fcout = fout.getChannel();
		// 创建缓冲区
		ByteBuffer buffer = ByteBuffer.allocate(2048);
		File[] files = new File(infile).listFiles();
		for(File file : files){
			FileInputStream fin = new FileInputStream(file);
			// 获取输入输出通道
			FileChannel fcin = fin.getChannel();
			while (true) {
				// clear方法重设缓冲区，使它可以接受读入的数据
				buffer.clear();
				// 从输入通道中将数据读到缓冲区
				int r = fcin.read(buffer);
				// read方法返回读取的字节数，可能为零，如果该通道已到达流的末尾，则返回-1
				if (r == -1) {
					break;
				}
				// flip方法让缓冲区可以将新读入的数据写入另一个通道
				buffer.flip();
				// 从输出通道中将数据写入缓冲区
				fcout.write(buffer);
			}
			fin.close();
			fcin.close();
		}
		fout.close();
		fcout.close();
		long et = System.currentTimeMillis();
		System.out.println(et - st);
	}

	public void copyFile(String infile, String outfile) throws Exception {
		long st = System.currentTimeMillis();
		FileInputStream a1 = new FileInputStream(infile);// 字节流输入流
		BufferedInputStream read = new BufferedInputStream(a1);// 字节流输入管道

		FileOutputStream b1 = new FileOutputStream(outfile);// 字节流输出流
		BufferedOutputStream out = new BufferedOutputStream(b1);// 字节流输出管道

		byte[] buf = new byte[2048];// 准备空的二进制数组
		int len;
		while ((len = read.read(buf)) != -1) {// 读取数据到数组 所读数据如果不是空的就循环
			out.write(buf);// 写入输出流指定文件 从0到数组这末多
		}
		read.close();// 关闭输入管道
		out.close();// 关闭输出管道
		long et = System.currentTimeMillis();
		System.out.println(et - st);
	}

	public void writeFile(String outfile, String data) throws Exception {
		long st = System.currentTimeMillis();
		FileOutputStream fout = new FileOutputStream(outfile);
		BufferedOutputStream out = new BufferedOutputStream(fout);
		for (int i = 0; i < 1000000; i++) {
			for(int j = 0; j<70;j++){
				out.write(data.getBytes());
			}
		}
		long et = System.currentTimeMillis();
		System.out.println(et - st);
	}
	public void writeFilenio(String outfile, String data) throws Exception {
		long st = System.currentTimeMillis();
		FileOutputStream fout = new FileOutputStream(outfile);
		FileChannel fcout = fout.getChannel();
		StringBuffer b = new StringBuffer();
		for (int i = 0; i < 1000000; i++) {
			for(int j = 0; j<70;j++){
				b.append(data);
				if(i%5000==0){
					fcout.write(ByteBuffer.wrap(b.toString().getBytes()));
					b.delete(0, b.length());
				}
			}
		}
		fcout.write(ByteBuffer.wrap(b.toString().getBytes()));
		long et = System.currentTimeMillis();
		System.out.println(et - st);
	}
}
class Taa extends Thread{
	private String s;
	public Taa(String a){
		s=a;
	}
	public  void run(){
		Logger log = Logger.getLogger("");
		log.error(s);
	}
}